package GUI;
import javax.swing.*;
import javax.swing.text.html.HTMLDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import 学生.Student;
import 数据库和网络.myDatebase;
import 试题和试卷.*;

public class reviseWindow extends JFrame{
    public static void main(String[] args) {
        new reviseWindow();
    }


    static test demo;
    public static JPanel mainPanel;
    public static JButton nextButton, preButton;
    static int currentIndex;
    static int selectionQuestionNum, definitionQuestionNum, fillingBlankQuestionNum, subjectiveQuestionNum;
    static ArrayList<revise> reviseList;
    static ArrayList<question> questionSet;
    static paper currentpaper;
    public Student currentstudent;
    public static String[][] resultSet;
    public static answerSheet ansboard;
    scoreWindow jumpingScore;


    public reviseWindow() {

        questionSet=new ArrayList<>();

        selectionQuestionNum=currentpaper.getSelectionNumber();
        definitionQuestionNum=currentpaper.getDefinitionNumber();
        fillingBlankQuestionNum = currentpaper.getFillingBlankNumber();
        subjectiveQuestionNum = currentpaper.getsubjectiveNumber();
        currentIndex =1;

        if(fillingBlankQuestionNum+subjectiveQuestionNum==0){
            JFrame unfinished=new JFrame();
            JPanel panel = new JPanel(new GridLayout(4, 1,20,20));
            JLabel successLab=new JLabel();
            JLabel lab2=new JLabel(" ");
            unfinished.setSize(200,150);
            Font bigFont = new Font("sanserif",Font.PLAIN,15);
            String a="无需批改！";
            successLab.setText(a);
            successLab.setFont(bigFont);
            panel.setSize(unfinished.getSize());
            panel.add(lab2);
            panel.add(successLab);
            unfinished.getContentPane().add(BorderLayout.CENTER,panel);
            unfinished.setLayout(new FlowLayout());//流式布局
            unfinished.setLocationRelativeTo(null);//居中

            unfinished.setTitle("在线考试平台");
            unfinished.setVisible(true);
            return;
        }else{
            myDatebase revisedQuery=new myDatebase("result_record");
            String sql,key="";

            try {
                sql=String.format("select studentID,is_revised from result_record.%s where studentID='"+currentstudent.getSchoolID()+"'",currentpaper.getName());
                revisedQuery.statement.execute(sql);
                revisedQuery.resultSet=revisedQuery.statement.executeQuery(sql);
                while (revisedQuery.resultSet.next()){
                    key=revisedQuery.resultSet.getString("is_revised");
                }
                if(key.equals("yes")){
                    JFrame unfinished=new JFrame();
                    JPanel panel = new JPanel(new GridLayout(4, 1,20,20));
                    JLabel successLab=new JLabel();
                    JLabel lab2=new JLabel(" ");
                    unfinished.setSize(200,150);
                    Font bigFont = new Font("sanserif",Font.PLAIN,15);
                    String a="您已批改完成！";
                    successLab.setText(a);
                    successLab.setFont(bigFont);
                    panel.setSize(unfinished.getSize());
                    panel.add(lab2);
                    panel.add(successLab);
                    unfinished.getContentPane().add(BorderLayout.CENTER,panel);
                    unfinished.setLayout(new FlowLayout());//流式布局
                    unfinished.setLocationRelativeTo(null);//居中

                    unfinished.setTitle("在线考试平台");
                    unfinished.setVisible(true);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        for(int i=selectionQuestionNum+definitionQuestionNum;i<currentpaper.getQuestionNumber();i++){
            questionSet.add(this.currentpaper.questionSet.get(i));
        }

        reviseList = new ArrayList<>();
        reviseList.add(new revise());

        for (int i = 1; i <= fillingBlankQuestionNum; i++) {
            reviseList.add(new reviseFillingBlank(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }
        for (int i = 1; i <= subjectiveQuestionNum; i++) {
            reviseList.add(new reviseSubjective(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }
        currentIndex = 1;



        nextButton = new JButton("下一题");
        nextButton.setBackground(Color.lightGray);
        nextButton.setBounds(380, 630, 80, 30);
        nextButton.addActionListener(new nextListener());

        preButton = new JButton("上一题");
        preButton.setBackground(Color.lightGray);
        preButton.setBounds(180, 630, 80, 30);
        preButton.addActionListener(new preListener());

        mainPanel = new JPanel();
        JMenuBar menuBar = new JMenuBar();
        Font bigFont = new Font("sanserif", Font.BOLD, 24);
        this.setJMenuBar(menuBar);
        //menuBar.setSize(this.getWidth(),30);

        JMenu submitMenu = new JMenu("Finish");
        JMenu quitMenu = new JMenu("Quit");
        JMenuItem submit = new JMenuItem("改卷完毕");
        JMenuItem quit = new JMenuItem("退出");
        menuBar.add(submitMenu);
        menuBar.add(quitMenu);
        submit.addActionListener(new submitListener());
        submitMenu.add(submit);
        quit.addActionListener(new quitListener());
        quitMenu.add(quit);

        mainPanel.setLayout(null);
        this.setTitle("试卷批改");
        this.setContentPane(mainPanel);
        this.setSize(630, 760);
        mainPanel.setSize(this.getSize());
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        reviseList.get(1).display();
        mainPanel.add(nextButton);
    }

    public reviseWindow(paper currentpaper,Student  currentstudent) {

                //System.out.println("请输入一个数字");
        //Scanner scan = new Scanner(System.in);
        this.currentstudent=currentstudent;
        selectionQuestionNum = currentpaper.getSelectionNumber();
        definitionQuestionNum = currentpaper.getDefinitionNumber();
        fillingBlankQuestionNum = currentpaper.getFillingBlankNumber();
        subjectiveQuestionNum = currentpaper.getsubjectiveNumber();

        if(fillingBlankQuestionNum+subjectiveQuestionNum==0){
            JFrame unfinished=new JFrame();
            JPanel panel = new JPanel(new GridLayout(4, 1,20,20));
            JLabel successLab=new JLabel();
            JLabel lab2=new JLabel(" ");
            unfinished.setSize(200,150);
            Font bigFont = new Font("sanserif",Font.PLAIN,15);
            String a="无需批改！";
            successLab.setText(a);
            successLab.setFont(bigFont);
            panel.setSize(unfinished.getSize());
            panel.add(lab2);
            panel.add(successLab);
            unfinished.getContentPane().add(BorderLayout.CENTER,panel);
            unfinished.setLayout(new FlowLayout());//流式布局
            unfinished.setLocationRelativeTo(null);//居中

            unfinished.setTitle("在线考试平台");
            unfinished.setVisible(true);

            return;
        }else{
            myDatebase revisedQuery=new myDatebase("result_record");
            String sql,key="";

            try {
                sql=String.format("select studentID,is_revised from result_record.%s where studentID='"+currentstudent.getSchoolID()+"'",currentpaper.getName());
                revisedQuery.statement.execute(sql);
                revisedQuery.resultSet=revisedQuery.statement.executeQuery(sql);
                while (revisedQuery.resultSet.next()){
                    key=revisedQuery.resultSet.getString("is_revised");
                }
                if(key!=null&&key.equals("yes")){
                    JFrame unfinished=new JFrame();
                    JPanel panel = new JPanel(new GridLayout(4, 1,20,20));
                    JLabel successLab=new JLabel();
                    JLabel lab2=new JLabel(" ");
                    unfinished.setSize(200,150);
                    Font bigFont = new Font("sanserif",Font.PLAIN,15);
                    String a="您已批改完成！";
                    successLab.setText(a);
                    successLab.setFont(bigFont);
                    panel.setSize(unfinished.getSize());
                    panel.add(lab2);
                    panel.add(successLab);
                    unfinished.getContentPane().add(BorderLayout.CENTER,panel);
                    unfinished.setLayout(new FlowLayout());//流式布局
                    unfinished.setLocationRelativeTo(null);//居中

                    unfinished.setTitle("在线考试平台");
                    unfinished.setVisible(true);
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        this.resultSet = new String[2][fillingBlankQuestionNum+subjectiveQuestionNum];
        this.currentpaper = currentpaper;
        questionSet=new ArrayList<>();

        for(int i=selectionQuestionNum+definitionQuestionNum;i<currentpaper.getQuestionNumber();i++){
            questionSet.add(this.currentpaper.questionSet.get(i));
        }

        for(int i=0;i<fillingBlankQuestionNum+subjectiveQuestionNum;i++){
            resultSet[0][i]=questionSet.get(i).getTitle();
            resultSet[1][i]="";
        }

        myDatebase saveDatebase=new myDatebase("result_record");
        try {
            String sql=String.format("select * from result_record.%s where studentID=%s",
                    currentpaper.getName(),currentstudent.getSchoolID());
            saveDatebase.statement.execute(sql);
            saveDatebase.resultSet=saveDatebase.statement.executeQuery(sql);
            while(saveDatebase.resultSet.next()){
                for(int i=0;i<fillingBlankQuestionNum+subjectiveQuestionNum;i++){
                    resultSet[1][i]=saveDatebase.resultSet.getString(resultSet[0][i]+"_current_answer");
                    System.out.println(resultSet[1][i]);
                }

            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            saveDatebase.close();
        }




        currentIndex =1;


        reviseList = new ArrayList<>();
        reviseList.add(new revise());

        for (int i = 1; i <= fillingBlankQuestionNum; i++) {
            reviseList.add(new reviseFillingBlank(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }
        for (int i = 1; i <= subjectiveQuestionNum; i++) {
            reviseList.add(new reviseSubjective(currentIndex, questionSet.get(currentIndex - 1)));
            currentIndex++;
        }


        currentIndex = 1;


        nextButton = new JButton("下一题");
        nextButton.setBackground(Color.lightGray);
        nextButton.setBounds(380, 630, 80, 30);
        nextButton.addActionListener(new nextListener());

        preButton = new JButton("上一题");
        preButton.setBackground(Color.lightGray);
        preButton.setBounds(180, 630, 80, 30);
        preButton.addActionListener(new preListener());

        mainPanel = new JPanel();
        JMenuBar menuBar = new JMenuBar();
        Font bigFont = new Font("sanserif", Font.BOLD, 24);
        this.setJMenuBar(menuBar);
        //menuBar.setSize(this.getWidth(),30);
        JMenu submitMenu = new JMenu("Finish");
        JMenu quitMenu = new JMenu("Quit");
        JMenuItem submit = new JMenuItem("改卷完毕");
        JMenuItem quit = new JMenuItem("退出");
        menuBar.add(submitMenu);
        menuBar.add(quitMenu);
        submit.addActionListener(new submitListener());
        submitMenu.add(submit);
        quit.addActionListener(new quitListener());
        quitMenu.add(quit);

        mainPanel.setLayout(null);
        this.setTitle("开始做题");
        this.setContentPane(mainPanel);
        this.setSize(630, 760);
        mainPanel.setSize(this.getSize());
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        /*addSelection((selectionQuestionItem) reviseList.get(1));*/
        reviseList.get(1).loadAnswer();
        reviseList.get(1).display();
        if (questionSet.size() > 1)
            mainPanel.add(nextButton);
        mainPanel.updateUI();

    }

    public reviseWindow(int index) {
        //System.out.println("请输入一个数字");
        //Scanner scan = new Scanner(System.in);



        selectionQuestionNum = currentpaper.getSelectionNumber();
        definitionQuestionNum = currentpaper.getDefinitionNumber();
        fillingBlankQuestionNum = currentpaper.getFillingBlankNumber();
        subjectiveQuestionNum = currentpaper.getsubjectiveNumber();


        currentIndex = 1;



        nextButton = new JButton("下一题");
        nextButton.setBackground(Color.lightGray);
        nextButton.setBounds(380, 630, 80, 30);
        nextButton.addActionListener(new nextListener());

        preButton = new JButton("上一题");
        preButton.setBackground(Color.lightGray);
        preButton.setBounds(180, 630, 80, 30);
        preButton.addActionListener(new preListener());

        mainPanel = new JPanel();
        JMenuBar menuBar = new JMenuBar();
        Font bigFont = new Font("sanserif", Font.BOLD, 24);
        this.setJMenuBar(menuBar);
        //menuBar.setSize(this.getWidth(),30);
        JMenu saveMenu = new JMenu("Save");
        JMenu displayMenu = new JMenu("Display");
        JMenu submitMenu = new JMenu("Submit");
        JMenu quitMenu = new JMenu("Quit");
        JMenuItem save = new JMenuItem("暂存");
        JMenuItem display = new JMenuItem("显示答题卡");
        JMenuItem submit = new JMenuItem("交卷");
        JMenuItem quit = new JMenuItem("退出");
        menuBar.add(saveMenu);
        menuBar.add(displayMenu);
        menuBar.add(submitMenu);
        menuBar.add(quitMenu);
        save.addActionListener(new saveListener());
        saveMenu.add(save);
        display.addActionListener(new displayListener());
        displayMenu.add(display);
        submit.addActionListener(new submitListener());
        submitMenu.add(submit);
        quit.addActionListener(new quitListener());

        quitMenu.add(quit);

        mainPanel.setLayout(null);
        this.setTitle("开始做题");
        this.setContentPane(mainPanel);
        this.setSize(630, 760);
        mainPanel.setSize(this.getSize());
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        mainPanel.add(nextButton);
        mainPanel.add(preButton);
        currentIndex = index;
        reviseList.get(currentIndex).loadAnswer();
        reviseList.get(currentIndex).display();

        if (index == 1) {
            mainPanel.remove(preButton);
        }
        if (index == fillingBlankQuestionNum+subjectiveQuestionNum) {
            mainPanel.remove(nextButton);
        }
        mainPanel.updateUI();
    }

    public class backListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面
            reviseWindow.this.dispose();
        }
    }

    public class saveListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            myDatebase saveDatebase=new myDatebase("result_record");
            String name=currentpaper.getName();
            try {
                for(int i=1;i<=fillingBlankQuestionNum+subjectiveQuestionNum;i++){
                    String sql=String.format("update %s SET %s_current_answer='"+resultSet[1][i-1]
                                    +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name,
                            resultSet[0][i-1]);
                    saveDatebase.statement.execute(sql);

                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally{
                saveDatebase.close();
            }

        }
    }

    public class displayListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            ansboard.setVisible(true);
        }
    }


    public class submitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int totalScore=0,fillScore=0,subScore=0;
            for(int i=1;i<reviseList.size();i++){
                System.out.println(reviseList.get(i).score.getText());
                if(reviseList.get(i).score.getText().isEmpty()) {

                    JFrame unfinished2=new JFrame();
                    JPanel panel = new JPanel(new GridLayout(4, 1,20,20));
                    JLabel successLab=new JLabel();
                    JLabel lab2=new JLabel(" ");
                    unfinished2.setSize(200,150);
                    Font bigFont = new Font("sanserif",Font.PLAIN,15);
                    String a="您还未完成批改！";
                    successLab.setText(a);
                    successLab.setFont(bigFont);
                    panel.setSize(unfinished2.getSize());
                    panel.add(lab2);
                    panel.add(successLab);
                    unfinished2.getContentPane().add(BorderLayout.CENTER,panel);
                    unfinished2.setLayout(new FlowLayout());//流式布局
                    unfinished2.setLocationRelativeTo(null);//居中

                    unfinished2.setTitle("在线考试平台");
                    unfinished2.setVisible(true);
                    return;
                    //这里加上一个弹出窗口就好了
                }else{
                    reviseList.get(i).currRank=Integer.parseInt(reviseList.get(i).score.getText());
                    totalScore+=reviseList.get(i).currRank;
                }
            }
            System.out.println(totalScore);

            for(int i=1;i<=fillingBlankQuestionNum+subjectiveQuestionNum;i++){
                if(reviseList.get(i).currentquestion.getType().equals("fillingBlank")){
                fillScore+=reviseList.get(i).currRank;}else{
                    subScore+=reviseList.get(i).currRank;
                }
            }
           /* for(int i=fillingBlankQuestionNum;i<subjectiveQuestionNum;i++){

            }*/
            System.out.println(fillScore+"     "+subScore);

            myDatebase saveDatebase=new myDatebase("result_record");

            try {
                String name=currentpaper.getName();
                String sql=String.format("update %s SET fillingblank_score='"+fillScore
                        +"'where studentID='"+ currentstudent.getSchoolID()+"'",name);
                saveDatebase.statement.execute(sql);
                sql=String.format("update %s SET subjective_score='"+subScore
                        +"'where studentID='"+ currentstudent.getSchoolID()+"'",name);
                saveDatebase.statement.execute(sql);
                sql=String.format("update result_record.%s SET is_revised='yes'"
                        +" where studentID='"+ currentstudent.getSchoolID()+"'",name);
                saveDatebase.statement.execute(sql);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }


            reviseWindow.this.dispose();
            answerSheet.thread.stop();


            mainPanel.updateUI();
        }
    }

    public class quitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            reviseWindow.this.dispose();

        }
    }

    public class preListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            mainPanel.add(nextButton);
            mainPanel.updateUI();
            reviseList.get(currentIndex--).remove();
            reviseList.get(currentIndex).display();
            reviseList.get(currentIndex).loadAnswer();
            if (currentIndex == 1) {
                mainPanel.remove(preButton);
            }

        }
    }

    public class nextListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            mainPanel.add(preButton);
            mainPanel.updateUI();
            reviseList.get(currentIndex++).remove();
            reviseList.get(currentIndex).display();
            reviseList.get(currentIndex).loadAnswer();
            if (currentIndex == fillingBlankQuestionNum+subjectiveQuestionNum) {
                mainPanel.remove(nextButton);
            }
        }
    }
}

